﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;

namespace EHTWebsiteProject
{
    public partial class CustomerProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    MembershipUser theUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                    ProfileBase theProfile = ProfileBase.Create(HttpContext.Current.User.Identity.Name);
                    txtFirstName.Text = theProfile.GetPropertyValue("FirstName").ToString();
                    txtLastName.Text = theProfile.GetPropertyValue("LastName").ToString();
                    txtAge.Text = theProfile.GetPropertyValue("Age").ToString();
                    txtPhone.Text = theProfile.GetPropertyValue("Phone").ToString();
                    txtFax.Text = theProfile.GetPropertyValue("Fax").ToString();
                    txtAddress.Text = theProfile.GetPropertyValue("Address").ToString();
                    txtCity.Text = theProfile.GetPropertyValue("City").ToString();
                    ddlRegion.SelectedValue = theProfile.GetPropertyValue("RegionID").ToString();
                    txtZipCode.Text = theProfile.GetPropertyValue("ZipCode").ToString();
                    txtEmail.Text = theProfile.GetPropertyValue("Email").ToString();
                    //theProfile.GetPropertyValue("SignatureUrl").ToString();
                }
                catch (Exception ex)
                {
                    ltrMessage.Text = "feil: " + ex.Message;
                }
            }
        }

        private Boolean ValidateData()
        {
            String message = "";
            Boolean isSuccess = true;

            if (ddlRegion.SelectedValue == "-1")
            {
                message += "Velge Region <br />";
                isSuccess = false;
            }

            if (!isSuccess)
                ltrMessage.Text = message;

            return isSuccess;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateData())
                return;

            Page.Validate();

            if (Page.IsValid)
            {
                try
                {
                    ProfileBase theProfile = ProfileBase.Create(HttpContext.Current.User.Identity.Name);
                    theProfile.SetPropertyValue("FirstName", this.txtFirstName.Text.Trim());
                    theProfile.SetPropertyValue("LastName", this.txtLastName.Text.Trim());
                    theProfile.SetPropertyValue("Age", this.txtAge.Text.Trim());
                    theProfile.SetPropertyValue("Phone", this.txtPhone.Text.Trim());
                    theProfile.SetPropertyValue("Fax", this.txtFax.Text.Trim());
                    theProfile.SetPropertyValue("Address", this.txtAddress.Text.Trim());
                    theProfile.SetPropertyValue("City", this.txtCity.Text.Trim());
                    theProfile.SetPropertyValue("RegionID", Int32.Parse(this.ddlRegion.SelectedValue));
                    theProfile.SetPropertyValue("ZipCode", this.txtZipCode.Text.Trim());
                    theProfile.SetPropertyValue("Email", this.txtEmail.Text.Trim());
                    theProfile.SetPropertyValue("SignatureUrl", "");//*
                    theProfile.Save();
                    ltrMessage.Text = "har endret seg";
                }
                catch (Exception ex)
                {
                    ltrMessage.Text = "feil: " + ex.Message;
                }
            }
        }
    }
}